home *** CD-ROM | disk | FTP | other *** search
- package symjava.sql;
-
- import java.io.PrintStream;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Properties;
- import java.util.Vector;
-
- public class DriverManager {
- private static Vector drivers = new Vector();
- private static int loginTimeout;
- private static PrintStream logStream = null;
- private static boolean initialized;
-
- public static synchronized Connection getConnection(String url, Properties info) throws SQLException {
- if (url == null) {
- throw new SQLException("The url cannot be null", "08001");
- } else {
- println("DriverManager.getConnection(\"" + url + "\")");
- if (!initialized) {
- initialize();
- }
-
- Object currentSecurityContext = getSecurityContext();
- SQLException reason = null;
-
- for(int i = 0; i < drivers.size(); ++i) {
- DriverInfo di = (DriverInfo)drivers.elementAt(i);
- if (di.securityContext != null && di.securityContext != currentSecurityContext) {
- println(" skipping: " + di);
- } else {
- try {
- println(" trying " + di);
- Connection result = di.driver.connect(url, info);
- if (result != null) {
- println("getConnection returning " + di);
- return result;
- }
- } catch (SQLException ex) {
- if (reason == null) {
- reason = ex;
- }
- }
- }
- }
-
- if (reason != null) {
- println("getConnection failed: " + reason);
- throw reason;
- } else {
- println("getConnection: no suitable driver");
- throw new SQLException("No suitable driver", "08001");
- }
- }
- }
-
- public static synchronized Connection getConnection(String url, String user, String password) throws SQLException {
- Properties info = new Properties();
- if (user != null) {
- ((Hashtable)info).put("user", user);
- }
-
- if (password != null) {
- ((Hashtable)info).put("password", password);
- }
-
- return getConnection(url, info);
- }
-
- public static synchronized Connection getConnection(String url) throws SQLException {
- Properties info = new Properties();
- return getConnection(url, info);
- }
-
- public static Driver getDriver(String url) throws SQLException {
- println("DriverManager.getDriver(\"" + url + "\")");
- if (!initialized) {
- initialize();
- }
-
- Object currentSecurityContext = getSecurityContext();
-
- for(int i = 0; i < drivers.size(); ++i) {
- DriverInfo di = (DriverInfo)drivers.elementAt(i);
- if (di.securityContext != null && di.securityContext != currentSecurityContext) {
- println(" skipping: " + di);
- } else {
- try {
- println(" trying " + di);
- if (di.driver.acceptsURL(url)) {
- println("getDriver returning " + di);
- return di.driver;
- }
- } catch (SQLException var4) {
- }
- }
- }
-
- println("getDriver: no suitable driver");
- throw new SQLException("No suitable driver", "08001");
- }
-
- public static synchronized void registerDriver(Driver driver) throws SQLException {
- if (!initialized) {
- initialize();
- }
-
- DriverInfo di = new DriverInfo();
- di.driver = driver;
- di.className = driver.getClass().getName();
- di.securityContext = getSecurityContext();
- drivers.addElement(di);
- println("registerDriver: " + di);
- }
-
- public static void deregisterDriver(Driver driver) throws SQLException {
- Object currentSecurityContext = getSecurityContext();
- println("DriverManager.deregisterDriver: " + driver);
- DriverInfo di = null;
-
- int i;
- for(i = 0; i < drivers.size(); ++i) {
- di = (DriverInfo)drivers.elementAt(i);
- if (di.driver == driver) {
- break;
- }
- }
-
- if (i >= drivers.size()) {
- println(" couldn't find driver to unload");
- } else if (currentSecurityContext != null && di.securityContext != currentSecurityContext) {
- throw new SecurityException();
- } else {
- drivers.removeElementAt(i);
- }
- }
-
- public static Enumeration getDrivers() {
- Vector result = new Vector();
- if (!initialized) {
- initialize();
- }
-
- Object currentSecurityContext = getSecurityContext();
-
- for(int i = 0; i < drivers.size(); ++i) {
- DriverInfo di = (DriverInfo)drivers.elementAt(i);
- if (di.securityContext != null && di.securityContext != currentSecurityContext) {
- println(" skipping: " + di);
- } else {
- result.addElement(di.driver);
- }
- }
-
- return result.elements();
- }
-
- public static void setLoginTimeout(int seconds) {
- loginTimeout = seconds;
- }
-
- public static int getLoginTimeout() {
- return loginTimeout;
- }
-
- public static void setLogStream(PrintStream out) {
- logStream = out;
- }
-
- public static PrintStream getLogStream() {
- return logStream;
- }
-
- public static void println(String message) {
- if (logStream != null) {
- logStream.println(message);
- }
-
- }
-
- private static Object getSecurityContext() {
- SecurityManager security = System.getSecurityManager();
- return security == null ? null : security.getSecurityContext();
- }
-
- private static void loadInitialDrivers() {
- String drivers;
- try {
- drivers = System.getProperty("jdbc.drivers");
- } catch (Exception var5) {
- drivers = null;
- }
-
- println("DriverManager.initialize: jdbc.drivers = " + drivers);
- if (drivers != null) {
- while(drivers.length() != 0) {
- int x = drivers.indexOf(58);
- String driver;
- if (x < 0) {
- driver = drivers;
- drivers = "";
- } else {
- driver = drivers.substring(0, x);
- drivers = drivers.substring(x + 1);
- }
-
- if (driver.length() != 0) {
- try {
- println("DriverManager.Initialize: loading " + driver);
- Class.forName(driver);
- } catch (Exception ex) {
- println("DriverManager.Initialize: load failed: " + ex);
- }
- }
- }
-
- }
- }
-
- static void initialize() {
- if (!initialized) {
- initialized = true;
- loadInitialDrivers();
- println("JDBC DriverManager initialized");
- }
- }
-
- private DriverManager() {
- }
- }
-